<template>
  <div class="base-echart">
    <div ref="ecahrtDivRef" :style="{ width: width, height: height }"></div>
  </div>
</template>
<script lang="ts" setup>
import { ref, onMounted, watchEffect } from "vue";
import * as echarts from "echarts";
import type { EChartsOption } from "echarts";
interface BaseEChartsProps {
  optionns: EChartsOption;
  width?: string;
  height?: string;
}
const props = withDefaults(defineProps<BaseEChartsProps>(), {
  width: "100%",
  height: "360",
});

const ecahrtDivRef = ref<HTMLElement>();
onMounted(() => {
  const echartInstance = echarts.init(ecahrtDivRef.value);
  window.addEventListener("resize", () => {
    echartInstance.resize();
  });
  const setOptions = (options: EChartsOption) => {
    echartInstance.setOption(options);
  };
  watchEffect(() => {
    setOptions(props.optionns);
  });
});
</script>
